home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / CommandNotFound / util.py < prev   
Text File  |  2009-10-13  |  2KB  |  68 lines

  1. # (c) Zygmunt Krynicki 2008
  2. # Licensed under GPL, see COPYING for the whole text
  3.  
  4. import sys
  5. import gettext
  6.  
  7. def no_gettext_for_you(message):
  8.     """This function is used instead of gettext when there are some locale problems"""
  9.     return message
  10.  
  11. def gettext_not_crashy(s):
  12.     """ The getext handling is confusing:
  13.         - gettext.gettext caused LP: #161159
  14.         - getext.lgettext causes LP: #282446
  15.         Here we just try both :(
  16.     """
  17.     try:
  18.         return gettext.lgettext(s)
  19.     except UnicodeEncodeError, e:
  20.         return gettext.gettext(s)
  21.  
  22. def setup_locale():
  23.     import locale
  24.     try:
  25.         locale.getpreferredencoding()
  26.         gettext.bindtextdomain("command-not-found", "/usr/share/locale")
  27.         gettext.textdomain("command-not-found")
  28.         gettext.install("command-not-found", unicode=True)
  29.         return gettext_not_crashy
  30.     except locale.Error:
  31.         #print "Warning: python was unable to setup locale!"
  32.         #print "Internationalizatio features will not be enabled."
  33.         return no_gettext_for_you
  34.  
  35. _ = gettext_wrapper = setup_locale()
  36.  
  37. def crash_guard(callback, bug_report_url, version):
  38.     """ Calls callback and catches all exceptions.
  39.     When something bad happens prints a long error message
  40.     with bug report information and exits the program"""
  41.     try:
  42.         try:
  43.             callback()
  44.         except Exception, ex:
  45.             print >>sys.stderr, _("Sorry, command-not-found has crashed! Please file a bug report at:")
  46.             print >>sys.stderr, bug_report_url
  47.             print >>sys.stderr, _("Please include the following information with the report:")
  48.             print >>sys.stderr
  49.             print >>sys.stderr, _("command-not-found version: %s") % version
  50.             print >>sys.stderr, _("Python version: %d.%d.%d %s %d") % sys.version_info
  51.             try:
  52.                 import subprocess
  53.                 subprocess.call(["lsb_release", "-i", "-d", "-r", "-c"], stdout=sys.stderr)
  54.             except (ImportError, OSError):
  55.                 pass
  56.             print >>sys.stderr, _("Exception information:")
  57.             print >>sys.stderr
  58.             print >>sys.stderr, ex
  59.             try:
  60.                 import traceback
  61.                 traceback.print_exc()
  62.             except ImportError:
  63.                 pass
  64.     finally:
  65.         sys.exit(127)
  66.  
  67. __all__ = ["gettext_wrapper", "crash_guard"]
  68.